package main.java.indi.zyj.coderecord;

import java.util.*;

public class FindSubsequences {

    List<List<Integer>> res = null;
    LinkedList<Integer> path = null;


    public List<List<Integer>> findSubsequences(int[] nums) {

        if (nums == null || nums.length == 0) {
            return res;
        }

        res = new ArrayList<>();
        path = new LinkedList<>();

        backtrack(nums, 0);

        return res;

    }

    private void backtrack(int[] nums, int start) {

        if (path.size() > 1) {
            res.add(new ArrayList<>(path));
        }

        // for循环代表横向的，也就是同层的，同一父节点的
        Set<Integer> hash = new HashSet<>();
        for (int i = start; i < nums.length; i++) {
            if (!path.isEmpty() && path.get(path.size() - 1) > nums[i]
                        || hash.contains(nums[i])) {
                continue;
            }
            hash.add(nums[i]);
            path.addLast(nums[i]);
            backtrack(nums, i + 1);
            path.removeLast();
        }

    }

}
